#include "widget.h"
////#include <QApplication>
#include <QtCore>
#include <QtGui>
#include <QtNetwork>
#include <QtXml>
#include <QtWebKit>
#include "batchnetworkaccessmanager.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

    QCoreApplication::setOrganizationDomain("akio.miyoshi");
    QCoreApplication::setOrganizationName("akio.miyoshi");
    QCoreApplication::setApplicationName("test0101");
    QSettings settings(
            QSettings::IniFormat, //QSettings::NativeFormat,
            QSettings::UserScope, // QSettings::SystemScope,
            QCoreApplication::organizationName(),
            QCoreApplication::applicationName()
            );
    //settings.setValue("twitpic/username", "dummy-user");
    //settings.setValue("twitpic/password", "dummy-pass");

    QWebSettings *webSettings = QWebSettings::globalSettings();
    webSettings->setAttribute(QWebSettings::AutoLoadImages, false);
    webSettings->setAttribute(QWebSettings::JavascriptEnabled, false); /**/

    qDebug() << "(1)";

    BatchNetworkAccessManager v_nam;
    QString v_cont_type;
    QByteArray v_result = v_nam.doGet(
                //"http://4u-beautyimg.com/?name=%E5%A4%8F%E5%B8%86"
                "http://4u-beautyimg.com/?name=夏帆"
                , &v_cont_type);
    qDebug() << "[v_cont_type]" << v_cont_type;
    qDebug() << "[v_result]" << v_result;

    qDebug() << "(2)";
    Widget w;

    QWebView *v_web_view = new QWebView();
    v_web_view->setHtml(QString::fromUtf8(v_result), QUrl("http://4u-beautyimg.com/"));

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(v_web_view);

    w.setLayout(layout);
    w.show();
    qDebug() << "(3)";

    QWebElement v_doc_elem = v_web_view->page()->mainFrame()->documentElement();
    qDebug() << "(4.1a)" << v_doc_elem.isNull();
    qDebug() << "(4.1b)" << v_doc_elem.localName();
    QWebElement v_body_elem = v_doc_elem.findFirst("body");
    qDebug() << "(4.2)" << v_body_elem.isNull();
    QWebElementCollection v_body_list = v_doc_elem.findAll("body");
    //QWebElementCollection v_body_list = v_doc_elem.findAll("head");
    //QWebElementCollection v_body_list = v_doc_elem.findAll("script");
    //QWebElementCollection v_body_list = v_doc_elem.findAll("*");
    qDebug() << "(4.2b)" << v_body_list.count();
    foreach(QWebElement v_elem, v_body_list)
    {
        qDebug() << "(4.2c)" << v_elem.localName();
    }
    QWebElementCollection v_entry_body_list = v_doc_elem.findAll("div.entry-body");
    qDebug() << "(4.2d)" << v_entry_body_list.count();
    foreach(QWebElement v_elem, v_entry_body_list)
    {
        qDebug() << "(5.1a)" << v_elem.localName();
        qDebug() << "(5.1b)" << v_elem.findFirst(".entry-photo").toOuterXml();
        qDebug() << "(5.1c)" << v_elem.findFirst(".entry-photo a").toOuterXml();
        qDebug() << "(5.1d)" << v_elem.findFirst(".entry-photo a img").toOuterXml();
        qDebug() << "(5.1d)" << v_elem.findFirst(".entry-photo a img").attribute("src");
    }
    return a.exec();
}
